<template>
  <div>
    <el-dialog :title="info.title" :visible.sync="info.isShow">
      <el-form :model="form">
        <el-form-item label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="form.title"></el-input>
        </el-form-item>

        <el-form-item label="上级菜单" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="请选择">
            <el-option label="顶级菜单" :value="0"></el-option>
            <!-- 遍历菜单名称 -->
            <el-option
              v-for="item in menus"
              :value="item.id"
              :key="item.id"
              :label="item.title"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio v-model="form.type" :label="1">目录</el-radio>
          <el-radio v-model="form.type" :label="2">菜单</el-radio>
        </el-form-item>

        <el-form-item
          label="菜单图标"
          :label-width="formLabelWidth"
          v-if="form.type === 1"
        >
          <el-select v-model="form.icon" placeholder="请选择">
            <!-- 遍历菜单名称 -->
            <el-option
              v-for="item in icons"
              :value="item"
              :key="item"
              :label="item"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单地址" :label-width="formLabelWidth" v-else>
          <el-select v-model="form.url" placeholder="请选择">
            <!-- 遍历菜单名称 -->
            <el-option
              v-for="item in urls"
              :value="item"
              :key="item"
              :label="item"
            >
            </el-option>
          </el-select>
        </el-form-item> 

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="blue"
            inactive-color="grey"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm" v-if="info.isAdd"
          >确 定</el-button
        >
        <el-button type="primary" @click="update" v-else
          >修 改</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { menuAdd, menuList, menuOne, menuUpdate } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      form: {
        pid: 0, //0顶级菜单
        title: "",
        icon: "",
        type: 1, //1:目录 2:菜单
        url: "",
        status: 1, //1.正常  2.禁用
      },
      formLabelWidth: "70px",
      icons: ["el-icon-setting","el-icon-user", "el-icon-star-off", "el-icon-help","el-icon-location","el-icon-s-goods"],
      urls: [
        "/index/home",
        "/index/menu",
        "/index/role",
        "/index/admin",
        "/index/categoods", 
        "/index/goodsSpec",
        "/index/goodsManager",
        "/index/vip",
        "/index/banners",
        "/index/seckill",
        "index/cs"
      ],
      menus: [],
    };
  },
  methods: {
    cancel() {
      this.info.isShow = false;
      this.form = {
        pid: 0, //0顶级菜单
        title: "",
        icon: "",
        type: 1, //1:目录 2:菜单
        url: "",
        status: 1, //1.正常  2.禁用
      };
    },
    confirm() {
      menuAdd(this.form).then((res) => {
        if (res.code === 200) {
          successAlert(res.msg);
          this.event.$emit("message");
        }
      });
      this.cancel();
    },
    goDetail(id){
        menuOne({id}).then(res=>{
            if(res.code===200){
                this.form=res.list;
                this.form.id=id;
            }
        })
    },
    update(){
        menuUpdate(this.form).then(res=>{
            if(res.code===200){
                successAlert(res.msg);
                this.cancel();
                this.event.$emit("message");
            }
        })
    }
  },
  mounted() {
    menuList().then((res) => {
      if (res.code === 200) {
        this.menus = res.list;
      }
    });
  },
};
</script>

<style scoped>
</style>